package cn.anc.httpcontrolutil;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import cn.anc.aonicardv.util.ui.CarSettingGuiHandler;

/**
 * Created by baobaoding on 2017/6/4.
 */

public class CarControlSettings {
    private static List<SettingItem> allSettingItems = new ArrayList<>();
    private static String TAG = CarControlSettings.class.getSimpleName();

    public static class SettingItemGuiResource {
        public int settingItemGroupId = -1;
        public int settingItemDisplayStrId = -1;
        public int settingItemDisplayIconId = -1;

        public SettingItemGuiResource(int groupId, int strId, int iconId) {
            settingItemGroupId = groupId;
            settingItemDisplayStrId = strId;
            settingItemDisplayIconId = iconId;
        }
    }

    public static class SettingOptionGuiResource {
        public int settingOptionDisplayStrId = -1;
        public int settingOptionDisplayIconId = -1;
        public String settingOptionKey = null;

        public SettingOptionGuiResource(String option_key, int strId, int iconId) {
            settingOptionDisplayStrId = strId;
            settingOptionDisplayIconId = iconId;
            settingOptionKey = option_key;
        }
    }

    public static class SettingItem implements Serializable {
        public String settingItemKey = null;
        public String settingItemValue = null;
        public List<String> settingItemOptions = new ArrayList<>();

        @Override
        public String toString() {
            return "SettingItem{" +
                    "settingItemKey='" + settingItemKey + '\'' +
                    ", settingItemValue='" + settingItemValue + '\'' +
                    ", settingItemOptions=" + settingItemOptions +
                    '}';
        }
    }

    private static onSetSettingItemGuiResource settingItemGuiResourceFunc = null;
    private static HashMap<String, SettingItemGuiResource> settingItemhashMap = new HashMap<>();
    private static HashMap<String, List<SettingOptionGuiResource>> settingOptionhashMap = new HashMap<>();

    public interface onSetSettingItemGuiResource {
        void pushSettingItemGuiResource(HashMap<String, SettingItemGuiResource> settingItemhashMap);

        void pushSettingOptionGuiResource(HashMap<String, List<SettingOptionGuiResource>> settingOptionhashMap);
    }

    public static void setSetSettingItemGuiResource(onSetSettingItemGuiResource handler) {
        settingItemGuiResourceFunc = handler;
    }

    public static void initGuiResource() {
        if (settingItemGuiResourceFunc != null) {
            settingItemGuiResourceFunc.pushSettingItemGuiResource(settingItemhashMap);
            settingItemGuiResourceFunc.pushSettingOptionGuiResource(settingOptionhashMap);
        }
    }

    public static List<SettingItem> getAllSettingItems() {
        return allSettingItems;
    }

    private static void addSettingItem(String key, String value, List<String> values) {
        SettingItem item = new SettingItem();
        item.settingItemKey = key;
        item.settingItemValue = value;
        item.settingItemOptions = values;
        allSettingItems.add(item);
    }

    public static SettingItem getSettingItem(String key) {
        for (int i = 0; i < allSettingItems.size(); i++) {
            if (allSettingItems.get(i).settingItemKey.equals(key)) {
                return allSettingItems.get(i);
            }
        }
        return null;
    }

    public static List<String> parseSettingOptions(String msg) {
        if (msg == null || msg.length() <= 0) {
            return null;
        }
        if (!msg.contains(",")) {
            List<String> temp = new ArrayList<>();
            temp.add(msg);
            return temp;
        }
        String[] temp = msg.split(",");
        return Arrays.asList(temp);
    }

    public static List<SettingItem> parseAllSettings(String msg) {
        try {
            allSettingItems.clear();
            if (msg == null || msg.length() <= 0) {
                return null;
            }
            JSONArray array = new JSONArray(msg);
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                Iterator<String> key_array = obj.keys();
                String key = null, value = null;
                String values = null;
                while (key_array.hasNext()) {
                    String tempKey = key_array.next();
                    if (tempKey.equals("options")) {
                        values = obj.getString(tempKey);
                    } else {
                        value = obj.getString(tempKey);
                        key = tempKey;
                    }
                }
                if (key != null) {
                    //APP端移除宽动态和Ev值设置  2023 02 15
                    if (!key.trim().equals(CarSettingGuiHandler.SETTING_ITEM_WDR)&&!key.trim().equals(CarSettingGuiHandler.SETTING_ITEM_EV)){
                    addSettingItem(key, value, parseSettingOptions(values));
                    }
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "parseAllSettings: " + ex.getMessage());
            return null;
        }
        return allSettingItems;
    }

    public static int setSettingItemCurValue(String key, String cur_value) {
        for (int i = 0; i < allSettingItems.size(); i++) {
            if (allSettingItems.get(i).settingItemKey.equals(key)) {
                allSettingItems.get(i).settingItemValue = cur_value;
                return 0;
            }
        }
        return -1;
    }

    public static SettingItemGuiResource getSettingItemGuiResource(String key) {
        if (settingItemhashMap.containsKey(key)) {
            return settingItemhashMap.get(key);
        }
        return null;
    }

    public static List<SettingItem> getSettingItemGuiResourceListByType(int type) {
        List<SettingItem> temp = new ArrayList<>();
        for (int i = 0; i < allSettingItems.size(); i++) {
            SettingItem settingItem = allSettingItems.get(i);
            if (settingItem != null) {
                SettingItemGuiResource settingItemGuiResource = getSettingItemGuiResource(settingItem.settingItemKey);
                if (settingItemGuiResource != null) {
                    if (settingItemGuiResource.settingItemGroupId == type) {
                        temp.add(allSettingItems.get(i));
                    }
                } else {
//                    Log.d("yD", "getSettingItemGuiResourceListByType: "+settingItem.settingItemKey);
                }
            }

        }
        return temp;
    }


    public static SettingOptionGuiResource getSettingOptionGuiResourceByKey(String key, String option) {

        List<SettingOptionGuiResource> temp = getSettingOptionGuiResource(key);
        if (temp == null)
            return null;
        for (int i = 0; i < temp.size(); i++) {
            if (temp.get(i).settingOptionKey.equals(option)) {
                return temp.get(i);
            }
        }
        return null;
    }

    public static List<SettingOptionGuiResource> getSettingOptionGuiResource(String key) {
        if (settingOptionhashMap.containsKey(key)) {
            return settingOptionhashMap.get(key);
        }
        return null;
    }

    public void clear() {
        allSettingItems.clear();
    }
}
